package com.zh.harmony.ormlitesample.slice;

import com.j256.ormlite.dao.ForeignCollection;
import com.zh.harmony.ormlitesample.ResourceTable;
import com.zh.harmony.ormlitesample.dao.ArticleDao;
import com.zh.harmony.ormlitesample.dao.UserDao;
import com.zh.harmony.ormlitesample.entity.ArticleEntity;
import com.zh.harmony.ormlitesample.entity.UserEntity;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private final UserDao mUserDao = new UserDao();
    private final ArticleDao mArticleDao = new ArticleDao();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Button insertBtn = (Button) findComponentById(ResourceTable.Id_insert_btn);
        Button deleteBtn = (Button) findComponentById(ResourceTable.Id_delete_btn);
        Button queryBtn = (Button) findComponentById(ResourceTable.Id_query_btn);
        Button updateBtn = (Button) findComponentById(ResourceTable.Id_update_btn);
        insertBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                boolean userResult = mUserDao.insert(new UserEntity(
                        null,
                        "Wally",
                        "13700000000",
                        18,
                        '1',
                        new Date(),
                        "广东省广州市天河区"
                ));
                UserEntity userEntity = mUserDao.queryLast();
                if (userResult) {
                    mArticleDao.insert(new ArticleEntity(
                            null,
                            "鉴往知来，跟着总书记学历史丨从一座纪念馆看克敌制胜法宝",
                            "解放战争中有一场战役，标志着“中国人民革命的军事形势达到一个新的转折点”。这就是辽沈战役。 8月16日，习近平总书记来到辽沈战役纪念馆，回顾东北解放战争历史和辽沈战役胜利进程。让我们跟随总书记的脚步，探寻这场“转折之战”的制胜密码。",
                            userEntity
                    ));
                    toast("新增成功");
                } else {
                    toast("新增失败");
                }
            }
        });
        deleteBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                List<UserEntity> userEntities = mUserDao.queryAll();
                if (userEntities.isEmpty()) {
                    toast("删除所有，成功：" + true);
                } else {
                    for (UserEntity userEntity : userEntities) {
                        //删除用户下的文章
                        ForeignCollection<ArticleEntity> articles = userEntity.getArticles();
                        for (ArticleEntity article : articles) {
                            mArticleDao.delete(article);
                        }
                        //删除用户
                        mUserDao.delete(userEntity);
                    }
                    toast("删除所有，成功：" + true);
                }
            }
        });
        queryBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                List<UserEntity> entityList = mUserDao.queryAll();
                if (entityList.isEmpty()) {
                    toast(Arrays.toString(entityList.toArray()));
                    return;
                }
                for (UserEntity userEntity : entityList) {
                    //List<ArticleEntity> articles = mArticleDao.queryByUserId(userEntity.getId());
                    ForeignCollection<ArticleEntity> articles = userEntity.getArticles();
                    toast("id => " + userEntity.getId() + " => age " + userEntity.getAge() + " => " + userEntity.getNickname() + " => " + articles.size() + "篇博客");
                }
            }
        });
        updateBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component view) {
                List<UserEntity> entities = mUserDao.queryByLikeNickname("Wally");
                if (entities.isEmpty()) {
                    toast("数据不存在");
                    return;
                }
                UserEntity entity = entities.get(0);
                entity.setAge(25);
                boolean isSuccess = mUserDao.update(entity);
                toast("更新年龄，成功：" + isSuccess);
            }
        });
    }

    private void toast(String msg) {
        ToastDialog toast = new ToastDialog(this);
        toast.setText(msg);
        toast.setAlignment(LayoutAlignment.BOTTOM);
        toast.setDuration(2000);
        toast.show();
    }
}